/**********************************************************************
*  Copyright (c) 2007 - Victor Jacobs - victor.jacobs@gmail.com
*
*  Permission is hereby granted, free of charge, to any person
*  obtaining a copy of this software and associated documentation
*  files (the "Software"), to deal in the Software without
*  restriction, including without limitation the rights to use,
*  copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the
*  Software is furnished to do so, subject to the following
*  conditions:
*
*  The above copyright notice and this permission notice shall be
*  included in all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using Vj.MathLibrary;
using System.Drawing;

namespace Vj.ShapeLibrary.Planar
{
    public class Plane : Shape
    {
        private int width;
        private int height;

        private bool drawFilled;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        public bool DrawFilled
        {
            get { return drawFilled; }
            set { drawFilled = value; }
        }

        public Plane(int width, int depth, bool fill)
        {
            this.width = width;
            this.height = depth;
            this.drawFilled = fill;
        }

        public void Draw()
        {
            base.SetMaterial();

            int h = this.height / 2;
            int w = this.width / 2;

            Gl.glPushMatrix();
            Gl.glTranslated(position.X, position.Y, position.Z);
            Gl.glRotated(rotationAngle, rotationVector.X, rotationVector.Y, rotationVector.Z);
            Gl.glScaled(scale.X, scale.Y, scale.Z);

            if (drawFilled)
            {
                Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
                Gl.glNormal3i(0, 1, 0);
                Gl.glVertex3i(-w, 0, -h);
                Gl.glNormal3i(0, 1, 0);
                Gl.glVertex3i(w, 0, -h);
                Gl.glNormal3i(0, 1, 0);
                Gl.glVertex3i(w, 0, h);
                Gl.glNormal3i(0, 1, 0);
                Gl.glVertex3i(-w, 0, h);
                Gl.glNormal3i(0, 1, 0);
                Gl.glVertex3i(-w, 0, -h);
                Gl.glEnd();
            }
            else
            {

                for (int i = -w; i < w; i++)
                {
                    Gl.glBegin(Gl.GL_LINES);
                    Gl.glVertex3i(i, 0, -h);
                    Gl.glVertex3i(i, 0, h);
                    Gl.glEnd();

                    for (int j = -h; j < h; j++)
                    {
                        Gl.glBegin(Gl.GL_LINES);
                        Gl.glVertex3i(-w, 0, j);
                        Gl.glVertex3i(w, 0, j);
                        Gl.glEnd();
                    }
                }

                Gl.glBegin(Gl.GL_LINES);
                Gl.glVertex3i(-w, 0, h);
                Gl.glVertex3i(w, 0, h);
                Gl.glEnd();

                Gl.glBegin(Gl.GL_LINES);
                Gl.glVertex3i(w, 0, -h);
                Gl.glVertex3i(w, 0, h);
                Gl.glEnd();
            }

            //float[] mat_diffuse2 = { 0.1f, 0.1f, 0.1f, 1.0f };
            //float[] mat_specular2 ={ 0.1f, 0.1f, 0.1f, 1.0f };
            //float[] mat_ambient2 = { 0.1f, 0.1f, 0.1f, 1.0f };

            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, mat_specular2);
            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse2);
            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient2);

            //Gl.glBegin(Gl.GL_LINES);
            //Gl.glVertex3d(0, 0, 0);
            //Gl.glVertex3d(0, 1.0f, 0);
            //Gl.glEnd();

            //float[] mat_diffuse3 = { 0.1f, 0.0f, 0.0f, 1.0f };
            //float[] mat_specular3 ={ 0.1f, 0.0f, 0.0f, 1.0f };
            //float[] mat_ambient3 = { 0.1f, 0.0f, 0.0f, 1.0f };

            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, mat_specular3);
            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse3);
            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient3);

            //Gl.glBegin(Gl.GL_LINES);
            //Gl.glVertex3d(0, 0, 0);
            //Gl.glVertex3d(1.0f, 0, 0);
            //Gl.glEnd();

            //float[] mat_diffuse4 = { 0.0f, 0.0f, 0.3f, 1.0f };
            //float[] mat_specular4 ={ 0.0f, 0.0f, 0.3f, 1.0f };
            //float[] mat_ambient4 = { 0.0f, 0.0f, 0.3f, 1.0f };

            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, mat_specular4);
            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse4);
            //Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient4);

            //Gl.glBegin(Gl.GL_LINES);
            //Gl.glVertex3d(0, 0, 0);
            //Gl.glVertex3d(0, 0, 1.0f);
            //Gl.glEnd();

            Gl.glPopMatrix();
        }
    }
}
